import modelExtend from 'dva-model-extend';
import { message } from 'dva';
import { query, create, remove, update } from '../services/sysConfig';
import { pageModel } from './common';
import { config } from '../configs';
import { delay } from './base';
import { isSuccess } from '../configs/error';

const { prefix } = config;

const defaultData = {
	searchCount: true,
	openSort: true,
	isAsc: false,
	showTotal: null,
	orderByField: 'ctime',
};

export default modelExtend(pageModel, {
	namespace: 'sysConfig',

	state: {
		currentItem: {},
		modalVisible: false,
		modalType: 'create',
		selectedRowKeys: [],
		isMotion: localStorage.getItem(`${prefix}userIsMotion`) === 'true',
	},

	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(location => {
				if (location.pathname === '/sysConfig') {
					dispatch({
						type: 'query',
						payload: location.query,
					});
				}
			});
		},
	},

	effects: {

		*query({ payload = defaultData }, { call, put }) {
			const data = yield call(query, { ...payload, ...defaultData });
			if (data) {
				const resultData = data.data;

				yield put({
					type: 'querySuccess',
					payload: {
						list: resultData.records,
						pagination: {
							current: Number(payload.current) || 1,
							pageSize: Number(resultData.size) || 10,
							total: resultData.total,
						},
					},
				});
			}
		},

		*'delete'({ payload }, { call, put, select }) {
			const data = yield call(remove, payload);
			if (isSuccess(data)) {
				yield put({ type: 'remove', id: payload });
				// yield put({ type: 'query' });
			}
		},

		// *'multiDelete'({ payload }, { call, put }) {
		// 	const data = yield call(remove, payload);
		// 	if (isSuccess(data)) {
		// 		yield put({ type: 'updateState', payload: { selectedRowKeys: [] } });
		// 		yield put({ type: 'query' });
		// 	} else {
		// 		throw data;
		// 	}
		// },

		*create({ payload }, { call, put, select, race }) {
			const creating = yield select(state => state.sysUser.creating);
			if (creating) {
				message.warning('请等待上一次创建成功');
				return;
			}
			const { data, timeout } = yield race({ // 超时控制
				data: call(create, payload),
				timeout: call(delay)
			});
			if (data.success) {
				yield put({ type: 'hideModal' });
				yield put({ type: 'query' });
			} else {
				throw data;
			}
		},

		*update({ payload }, { select, call, put }) {
			const id = yield select(({ sysConfig }) => sysConfig.currentItem.id);
			const newUser = { ...payload, id };
			const data = yield call(update, newUser);
			if (data.success) {
				yield put({ type: 'hideModal' });
				yield put({ type: 'query' });
			} else {
				throw data;
			}
		},

	},

	reducers: {

		showModal(state, { payload }) {
			return { ...state, ...payload, modalVisible: true };
		},

		hideModal(state) {
			return { ...state, modalVisible: false };
		},

		switchIsMotion(state) {
			localStorage.setItem(`${prefix}userIsMotion`, !state.isMotion);
			return { ...state, isMotion: !state.isMotion };
		},

		remove(state, { id }) {
			const { list, selectedRowKeys, ...other } = state;
			return {
				...other,
				selectedRowKeys: selectedRowKeys.filter(v => v !== id),
				list: list.filter(l => l.id !== id)
			};
		}
	},
});
